package com.wind;

import java.util.ArrayList;
import java.util.List;

public class WindConstraintNotNull extends WindConstraint {
	
	private static WindConstraint instance = new WindConstraintNotNull();
	
	public static WindConstraint getInstance() {
		return instance;
	}
	
	public String getCode() {
		return "wind.datastore.constraint.notNull";
	}

	public List<WindConstraintViolation> checkForInsert(WindEntity entity, WindTypeAttribute attribute) {
		return check(entity, attribute);
	}
	
	public List<WindConstraintViolation> checkForUpdate(WindEntity entity, WindTypeAttribute attribute) {	
		return check(entity, attribute);
	}
	
	public List<WindConstraintViolation> check(WindEntity entity, WindTypeAttribute attribute) {
		List<WindConstraintViolation> violations = new ArrayList<WindConstraintViolation>();
		
		String value = entity.getAttribute(attribute.getCode());
		if(isNull(value)) {
			WindConstraintViolation violation = new WindConstraintViolation(this, entity, attribute);
			violations.add(violation);
		}
		
		return violations;
	}
	
	private boolean isNull(Object obj) {
		boolean isNull = false;
		
		if(obj==null) {
			isNull = true;
		} else {
			if(obj instanceof String) {
				if(String.valueOf(obj).length()<=0) {
					isNull = true;
				}
			}
		}
		
		return isNull;
	}
}
